<?
/**
* Search controller - This makes the search possible to the entire site.
*
* @author eae
* @link <site_url>/search
*/
Class Search extends Controller{

    function __construct()
    {
        parent::Controller();

        $this->load->model('searchmodel');
        $this->load->model('glossarymodel');

    }
    
    function index()
    {

        //load page description
        $this->load->model('pageheadersmodel'); 
        $data["pagedescritpion"] = $this->pageheadersmodel->get_header('faq');

        $data["search_areas"] = $this->get_search_areas();  

        //document title
        $data["document_title"] = DOCUMENT_TITLE . "Search";

        $this->load->view('website/default/head', $data); 
        $this->load->view('website/default/header');  
        $this->load->view('website/search/search', $data);   
        $this->load->view('website/default/footer');   

    } 

    // display search results.
    public function results($filter = FALSE)
    {     

        $search_string = $this->get_search_string(); 
        if ($search_string === FALSE)
            redirect('/search/');
                                                       

        $search_areas = $this->get_search_areas();

        if ($filter == FALSE)
            $results = $this->get_results($search_string, $search_areas);
        else 
            $results = $this->get_results($search_string, array($filter));

        //pass it to view
        $data["results"] = $results;     
        $data["search_areas"] = $search_areas;     
        $data["search_string"] = $search_string; 
        $data["filter"] = $filter;     

        //document title
        $data["document_title"] = DOCUMENT_TITLE . "Search results for '" .$search_string."'";

        //load view partials
        $this->load->view('website/default/head', $data); 
        $this->load->view('website/default/header');  
        $this->load->view('website/search/search_results', $data);   
        $this->load->view('website/default/footer');   
  
    }

    private function get_search_areas() { 

        if (isset($_POST['set_search_fields'])) {

            $search_areas = array();

            if (isset($_POST['products'])) 
                $search_areas[] = 'products';

            if (isset($_POST['product_categories'])) 
                $search_areas[] = 'product_categories';                               

            if (isset($_POST['news']))
                $search_areas[] = 'news';

            if (isset($_POST['publicity'])) 
                $search_areas[] = 'publicity';

            if (isset($_POST['portfolio']))
                $search_areas[] = 'portfolio';

            if (isset($_POST['glossary'])) 
                $search_areas[] = 'glossary';

            if (isset($_POST['articles']))  
                $search_areas[] = 'articles';

            if (isset($_POST['exhibitions'])) 
                $search_areas[] = 'exhibitions';

            # put it 
            $this->session->set_userdata('search_areas', $search_areas); 

        } else {

            # get it  
            $search_areas = $this->session->userdata('search_areas');
            
            if ($search_areas === FALSE) $search_areas = array();

        }
        
        return $search_areas;

    }

    private function get_search_string() {   

        if (isset($_POST['search_submit'])) {
            
            $search_string = strtolower(trim($this->input->post('search', TRUE)));

            if ($search_string == '') 
                return FALSE;
            else 
                //$this->searchmodel->add_to_history($search_string);   
                # put it 
                $this->session->set_userdata('search_string', $search_string);

        } else {

            $search_string = $this->session->userdata('search_string');

        }
                                                                      
        return $search_string;

    }

    //returns an array of results. Each result is an array containing 'name' and an array of 'items'
    private function get_results($search_string = '', $search_areas = array()){

        $results = array();
        if (in_array('products', $search_areas)) {
            $items = $this->searchmodel->search_products($search_string);                  
            if (!empty($items)) 
                $results[] = array('label' => 'Products', 'id' => 'products', 'items' => $items);
        }

        if (in_array('product_categories', $search_areas)) {
            $items = $this->searchmodel->search_product_categories($search_string);
            if (!empty($items)) 
                $results[] = array('label' => 'Product Categories', 'id'=>'product_categories', 'items' => $items);
        }

        if (in_array('news', $search_areas)) {
            $items = $this->searchmodel->search_news($search_string);
            if (!empty($items)) 
                $results[] = array('label' => 'News', 'id'=>'news', 'items' => $items);
        }

        if (in_array('publicity', $search_areas)) {
            $items = $this->searchmodel->search_publicity($search_string);
            if (!empty($items)) 
                $results[] = array('label' => 'Publicity', 'id' => 'publicity', 'items' => $items);
        }

        if (in_array('portfolio', $search_areas)) {
            $items = $this->searchmodel->search_portfolio($search_string);
            if (!empty($items)) 
                $results[] = array('label' => 'Portfolio', 'id' => 'portfolio', 'items' => $items);
        }

        if (in_array('glossary', $search_areas)) {
            $items = $this->searchmodel->search_glossary($search_string);
            if (!empty($items)) 
                $results[] = array('label' => 'Glossary', 'id' => 'glossary', 'items' => $items);
        }

        if (in_array('articles', $search_areas)) {
            $items = $this->searchmodel->search_articles($search_string);
            if (!empty($items)) 
                $results[] = array('label' => 'Articles', 'id' => 'articles', 'items' => $items);
        }

        if (in_array('exhibitions', $search_areas)) {
            $items = $this->searchmodel->search_exhibitions($search_string);
            if (!empty($items)) 
                $results[] = array('label' => 'Exhibitions', 'id' => 'exhibitions', 'items' => $items);
        }

        return $results;

    }


}
?>